#ifndef DINOGAME_H
#define DINOGAME_H

#include <QWidget>
#include<Qpainter>
#include<QTimer>
#include <QKeyEvent>
#include <QVector>
#include <QRandomGenerator>

QT_BEGIN_NAMESPACE
namespace Ui {
class dinogame;
}
QT_END_NAMESPACE
class dinogame : public QWidget
{
    Q_OBJECT

public:
    dinogame(QWidget *parent = nullptr);
    ~dinogame();
protected:
    void paintEvent(QPaintEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;

private slots:
    void startGame();
    void updateGame();

private:
     Ui::dinogame *ui;
    QVector<QPixmap> dinostand;//恐龙站立
    QVector<QPixmap> dinodown;//恐龙蹲
    QVector<QPixmap> bird;//鸟
    int currentframe1=0,currentframe2=0,currentframe3=0;//当前帧
    int framecounter1=0,framecounter2=0,framecounter3=0;//计数器
    const int frame_delay=5;//动画更新速度
    QPixmap background;
    QPixmap cactus;//仙人掌
    QPixmap ground;//地面
    int dinoY;
    bool isjumping;
    int jumpvelocity;
    bool isDucking = false;
    bool downKeyPressed = false; // 下键是否被按住
    const int DINO_STAND_HEIGHT = 50; // 站立高度
    const int DINO_DUCK_HEIGHT = 30;  // 下蹲高度
    const int DINO_STAND_Y = 250;//站立时的Y坐标
    const int DINO_DUCK_Y = 270;// 下蹲时的Y坐标（比站立时低）
    int depleteheight;//鸟的额外高度
    int randval;//额外高度出现的概率；
    int obstacleSpawnTimer;
    bool gameover;
    struct obstacle{
        int x;
        int y;
        int width;
        int height;
        int speed;
        int type;
        bool active;
    };
    QVector<obstacle> obstacles;
    QTimer *timer;
    int score;
public:
    void loadimages();
    void spawnObstacle();    // 生成新障碍物
    bool checkCollision(const obstacle &obs); // 碰撞检测
    void resetGame();        // 重置游戏状态
};
#endif // DINOGAME_H
